home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / hlcsg / wadinclude.cpp < prev    next >
C/C++ Source or Header  |  2002-11-10  |  5KB  |  201 lines

  1. #include "csg.h"
  2.  
  3. #ifdef HAVE_UNISTD_E
  4. #include <unistd.h>
  5. #endif
  6.  
  7. void            LoadWadincludeFile(const char* const filename)
  8. {
  9.     char*           fname;
  10.     int             i, x;
  11.     char*           pData = NULL;
  12.     char*           pszData;
  13.     unsigned        len = strlen(filename) + 5;
  14.  
  15.     fname = (char*)Alloc(len);
  16.     safe_snprintf(fname, len, "%s.wic", filename);
  17.  
  18.     if (q_exists(fname))
  19.     {
  20.         i = LoadFile(fname, &pData);
  21.  
  22.         if (i == 0)
  23.         {
  24.             goto LoadWadincludeFileReturn;
  25.         }
  26.     }
  27.     else
  28.     {
  29.         Warning("WadInclude file %s does not exist", fname);
  30.         goto LoadWadincludeFileReturn;
  31.     }
  32.  
  33.     for (pszData = pData, x = 0; x < i; x++)
  34.     {
  35.         if (pData[x] == ';')
  36.         {
  37.             pData[x] = 0;
  38.             g_WadInclude.push_back(pszData);
  39.             pszData = pData + x + 1;
  40.         }
  41.     }
  42.  
  43. LoadWadincludeFileReturn:;
  44.     Free(fname);
  45.     if (pData)
  46.     {
  47.         Free(pData);
  48.     }
  49. }
  50.  
  51. void            SaveWadincludeFile(const char* const filename)
  52. {
  53.     char*           fname;
  54.     FILE*           file;
  55.     int             x;
  56.     unsigned        len = strlen(filename) + 5;
  57.  
  58.     fname = (char*)Alloc(len);
  59.     safe_snprintf(fname, len, "%s.wic", filename);
  60.  
  61.     unlink(fname);
  62.  
  63.     file = SafeOpenWrite(fname);
  64.  
  65.     WadInclude_i it;
  66.     for (it = g_WadInclude.begin(); it != g_WadInclude.end(); it++)
  67.     {
  68.         x = it->size();
  69.         if (x)
  70.         {
  71.             SafeWrite(file, it->c_str(), x);
  72.             SafeWrite(file, ";", 1);
  73.         }
  74.     }
  75.  
  76.     Free(fname);
  77.     fclose(file);
  78. }
  79.  
  80. // this function is called in place of tex_initfromwad for onlyents compiles
  81. void            HandleWadinclude()
  82. {
  83.     int             i;
  84.     char            szTmpWad[1024]; // arbitrary, but needs to be large.
  85.     char*           pszWadFile;
  86.     wadpath_t*      currentwad;
  87.  
  88.     Log("\n"); // looks cleaner
  89.  
  90.     szTmpWad[0] = 0;
  91.  
  92. #ifdef HLCSG_AUTOWAD
  93.     if (g_bWadAutoDetect)
  94.     {
  95.         autowad_UpdateUsedWads();
  96.     }
  97. #endif
  98.  
  99.     // for eachwadpath
  100.     for (i = 0; i < g_iNumWadPaths; i++)
  101.     {
  102.         bool            bExcludeThisWad = false;
  103.  
  104.         currentwad = g_pWadPaths[i];
  105.         pszWadFile = currentwad->path;
  106.         
  107. #ifdef HLCSG_AUTOWAD/*
  108.     #ifdef _DEBUG
  109.         Log("[dbg] HandleWIC: attempting to parse wad '%s'\n", currentwad->path);
  110.     #endif*/
  111.         if (g_bWadAutoDetect && !currentwad->usedtextures)
  112.             continue;/*
  113.     #ifdef _DEBUG
  114.         Log("[dbg] HandleWIC: parsing wad\n");
  115.     #endif*/
  116. #endif // HLCSG_AUTOWAD
  117.  
  118.         // look and see if we're supposed to include the textures from this WAD in the bsp.
  119.         WadInclude_i it;
  120.         for (it = g_WadInclude.begin(); it != g_WadInclude.end(); it++)
  121.         {
  122.             if (stristr(pszWadFile, it->c_str()))
  123.             {
  124.                 Log("Including Wadfile: %s\n", pszWadFile);
  125.                 bExcludeThisWad = true;             // wadincluding this one
  126.             }
  127.         }
  128.  
  129.         if (!bExcludeThisWad)
  130.         {
  131.             Log("Using Wadfile: %s\n", pszWadFile);
  132.             safe_snprintf(szTmpWad, 1024, "%s%s;", szTmpWad, pszWadFile);
  133.         }
  134.     }
  135.  
  136.     Log("\"wad\" is \"%s\"\n", szTmpWad);
  137.  
  138.     SetKeyValue(&g_entities[0], "wad", szTmpWad);
  139.  
  140.     Log("\n");
  141.     CheckFatal();
  142. }
  143.  
  144. #if 0
  145. void            HandleWadinclude()
  146. {
  147.     // Code somewhat copied from TEX_InitFromWad()
  148.  
  149.     char            szTmpPath[MAXTOKEN];
  150.     char            szNewWad[MAXTOKEN];
  151.     char*           pszWadFile;
  152.     bool            bExcludeThisWad;
  153.  
  154.     const char*     path = ValueForKey(&g_entities[0], "wad");
  155.     
  156.     szNewWad[0] = 0;
  157.     safe_strncpy(szTmpPath, path, MAXTOKEN);
  158.  
  159.     // temporary kludge so we don't have to deal with no occurances of a semicolon
  160.     //  in the path name ..
  161.     if (strchr(szTmpPath, ';') == NULL)
  162.     {
  163.         safe_strncat(szTmpPath, ";", MAXTOKEN);
  164.     }
  165.  
  166.     pszWadFile = strtok(szTmpPath, ";");
  167.  
  168.     while (pszWadFile)
  169.     {
  170.         bExcludeThisWad = false;
  171.  
  172.         // look and see if we're supposed to include the textures from this WAD in the bsp.
  173.         WadInclude_i it;
  174.         for (it = g_WadInclude.begin(); it != g_WadInclude.end(); it++)
  175.         {
  176.             if (stristr(pszWadFile, it->c_str()))
  177.             {
  178.                 Log("Embedding textures from WAD File [%s] into BSP\n", pszWadFile);
  179.                 bExcludeThisWad = true;
  180.             }
  181.         }
  182.  
  183.         if (!bExcludeThisWad)
  184.         {
  185.             safe_strncat(szNewWad, pszWadFile, MAXTOKEN);
  186.             safe_strncat(szNewWad, ";", MAXTOKEN);
  187.         }
  188.  
  189.         if (!bExcludeThisWad)
  190.         {
  191.             Log("Using WAD File: %s\n", pszWadFile);
  192.         }
  193.  
  194.         // next wad file
  195.         pszWadFile = strtok(NULL, ";");
  196.     }
  197.  
  198.     SetKeyValue(&g_entities[0], "wad", szNewWad);
  199. }
  200.  
  201. #endif